package org.taptwo.android.widget.viewflow.example;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Toast;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.GVIcon;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.tools.controls.layout.DashboardLayout;

/**
 * Copyright © 1998 - 2012 Epoint. All Rights Reserved.
 * @author liyc
 * @time 2012-11-27 下午11:19:27
 * @annotation
 */
public class MainViewCircleAdapter extends BaseAdapter{

	private LayoutInflater mInflater;
	private SuperView context;
	private List<GVIcon> iconList;

	public MainViewCircleAdapter(SuperView context,List<GVIcon> iconList) {
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.context = context;
		this.iconList = iconList;
	}

	@Override
	public int getCount() {
		int num = iconList.size();
		Double a = Double.valueOf(num);
		Double page = a/6;
		int count = (int) Math.ceil(page);
		return count;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		DashboardLayout dl = (DashboardLayout) mInflater.inflate(R.layout.dashboardgroup, null);
		int pageicon ;
		if((iconList.size()-(position+1)*6)<0)
		{
			pageicon = iconList.size()-position*6;
		}
		else{
			pageicon = 6;
		}
		for(int j=0;j<pageicon;j++)
		{
			final Button btn = (Button) mInflater.inflate(R.layout.btn_mainviewicon, null);
			final int iconPosition = position*6+j;
			final GVIcon gvicon = iconList.get(iconPosition);
			btn.setText(gvicon.iconTitle);
			Drawable d = context.getResources().getDrawable(gvicon.iconImage);
			d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
			btn.setCompoundDrawables(null, d, null, null);
			if(context.getBackgroundStr().equals("background"))
			{
				btn.setTextColor(context.getResources().getColor(R.color.white));
			}
			else{
				btn.setTextColor(context.getResources().getColor(R.color.main_button_text));
			}
			btn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					//Toast.makeText(context, btn.getText(), Toast.LENGTH_SHORT).show();
					try{
						context.startActivity(gvicon.intent);
					}catch(Exception e)
					{
						Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
					}
				}
			});


			/*btn.setOnLongClickListener(new OnLongClickListener() {

				@Override
				public boolean onLongClick(View v) {
					iconList.remove(iconPosition);
					notifyDataSetChanged();
					return true;
				}
			});*/
			dl.addView(btn);
		}
		if(pageicon<6)
		{
			for(int i=0;i<6-pageicon;i++)
			{
				Button btn = new Button(context);
				btn.setVisibility(View.INVISIBLE);
				dl.addView(btn);
			}
		}
		return dl;
	}

}
